package dingyunchi.job.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import dingyunchi.job.dao.ScoreMapper;
import dingyunchi.job.entity.Score;
import dingyunchi.job.service.ScoreService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ScoreServiceImpl implements ScoreService {
    private final ScoreMapper scoreMapper;

    public ScoreServiceImpl(ScoreMapper scoreMapper) {
        this.scoreMapper = scoreMapper;
    }

    @Override
    public Score save(Score score) {
        if (score.getScore() < 0 || score.getScore() > 100) {
            throw new IllegalArgumentException("成绩应在0到100之间");
        }
        scoreMapper.insert(score);
        return score;
    }

    @Override
    public Score findById(Long id) {
        return scoreMapper.selectById(id);
    }

    @Override
    public List<Score> findAll() {
        return scoreMapper.selectList(null);
    }

    @Override
    public void update(Score score) {
        if (score.getScore() < 0 || score.getScore() > 100) {
            throw new IllegalArgumentException("成绩应在0到100之间");
        }
        scoreMapper.updateById(score);
    }

    @Override
    public void delete(Long id) {
        scoreMapper.deleteById(id);
    }

    @Override
    public List<Score> getScoreRanking() {
        QueryWrapper<Score> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("score");
        Page<Score> page = new Page<>(1, 100); // 假设取前100条
        IPage<Score> scorePage = scoreMapper.selectPage(page, wrapper);
        return scorePage.getRecords();
    }
}